JavaScript Math Object

    JavaScript provides an Inbuilt Math object to handle mathematic tasks with numbers, and in this tutorial, we will learn how to use some of the Math object properties and methods to perform those tasks.

    The Math object

    The Math is a static object which means we do not need to initialize it with the new constructor. Instead, we can directly use Math all properties and method using the Math name, without creating its object.

    Math Properties Constant values

    The Math object supports many properties that are used to represent constant values. There are 8 number constant values provided as the Math properties.

    Syntax

    Math.property_name

    Example

    <script>
        euler = Math.E;   // return Euler's number
        pi  = Math.PI;   // retrun pi value
        sqrt2   = Math.SQRT2;  // retrun square root of 2 
        sqrt1_2   = Math.SQRT1_2;   // return square root of 1/2
        log2   = Math.LN2; // return logarithm of 2
        log10 = Math.LN10;   //retrun logarithm of 10
        log2e  = Math.LOG2E;  //retrun base 2 log value of E
        log10e  = Math.LOG10E;  // return base 10 log value of E
    
        console.log(euler);
        console.log(pi);
        console.log(sqrt2);
        console.log(sqrt1_2);
        console.log(log2);
        console.log(log10);
        console.log(log2e);
        console.log(log10e);   
    </script>

    Output

    2.718281828459045
    3.141592653589793
    1.4142135623730951
    0.7071067811865476
    0.6931471805599453
    2.302585092994046
    1.4426950408889634
    0.4342944819032518

    Math Methods

    Now let's see some of the Math important methods that we often use during JavaScript programming.

    Number to Integer

    There are 4 common Math methods that can be used to round up a floating-point or decimal number to an integer number.

    • Math.round(number)
    • Math.ceil(number)
    • Math.floor(number)
    • Math.trunc(number)

    Math.round() method

    The round(num) method accepts a number num as a parameter and return the nearest integer value of the number num .

    Example

    <script>
        console.log(Math.round(1.5));  //2
        console.log(Math.round(20.3));  //20
        console.log(Math.round(3.9));   //4
        console.log(Math.round(-3.1));   //-3
        console.log(Math.round(3.1));   //3
        console.log(Math.round(3));     //3
    </script>

    Math.ceil() method

    The ceil(num) method also accepts a number num as a parameter and round up the number to its nearest large Integer number.

    Example

    <script>
        console.log(Math.ceil(1.5));  //2
        console.log(Math.ceil(20.3));  //21
        console.log(Math.ceil(3.9));   //4
        console.log(Math.ceil(-3.1));   //-3
        console.log(Math.ceil(3.1));   //4
        console.log(Math.ceil(3.0));     //3
    </script>

    Math.floor() method

    The Math.floor(num) method accepts a number num as a parameter and return a round down integer Number of num .

    Example

    <script>
        console.log(Math.floor(1.5));  //1
        console.log(Math.floor(20.3));  //20
        console.log(Math.floor(3.9));   //3
        console.log(Math.floor(-3.1));   //-4
        console.log(Math.floor(3.1));   //3
        console.log(Math.floor(3.0));     //3
    </script>

    Math.trunc() method

    The trunc(num.ddd) method remove the decimal part and only return the integer part of num.ddd . We can use this method to convert any decimal number to its integer value without rounding the number.

    Example

    <script>
        console.log(Math.trunc(1.5));  //1
        console.log(Math.trunc(20.3));  //20
        console.log(Math.trunc(3.9));   //3
        console.log(Math.trunc(-3.1));   //-3
        console.log(Math.trunc(3.1));   //3
        console.log(Math.trunc(3.0));     //3
    </script>

    Math.sign() method

    The sign(num) method is used to check the positive, negative or null sign on the number num . If the value of num is negative the sign() method will return -1 if the value of num is positive the sign() will return 1 for num 0 value it returns 0.

    Example

    <script>
        console.log(Math.sign(-2));   //-1
        console.log(Math.sign(2));    //1
        console.log(Math.sign(0));    //0
    </script>

    Math.pow() method

    The Math.pow(a,b) is used to find the power of a raise to the b .

    Example

    <script>
        console.log(Math.pow(3,2));   //9
        console.log(Math.pow(4,3));    //64
        console.log(Math.pow(5,2));    //25
    </script>

    Math.sqrt() method

    The sqrt(num) method will return the square root of number num .

    Example

    <script>
        console.log(Math.sqrt(64));   //8
        console.log(Math.sqrt(50));    //7.0710678118654755
        console.log(Math.sqrt(100));    //10
    </script>

    Math.abs() method

    The abs(num) method returns the absolute positive value of the specified parameter num .

    Example

    <script>
        console.log(Math.abs(-64));   //64
        console.log(Math.abs(-50));    //50
        console.log(Math.abs(-100));    //100
    </script>

    Math.sin() method

    The sin(angle_radian) method return the sin value (-1 to 1) of the specified radian angle. By default, the sin() method accepts value in radian but we can also convert the radian value to the degree with the following formula. Angle in radians = Angle in degrees x PI / 180.

    Example

    <script>
        // in radian
        console.log(Math.sin(90));   //0.8939966636005579
    
        //in degree
        console.log(Math.sin(90*Math.PI/180));    //1
    </script>

    Math.cos() method

    The cos(angle_radian) method returns the cosine value (-1 to 1) of the specified radian angle. By default, the cos() method accepts value in radian but we can also convert the radian value to the degree with the following formula. Angle in radians = Angle in degrees x PI / 180.

    Example

    <script>
        // in radian
        console.log(Math.cos(180));   //-0.5984600690578581
    
        //in degree
        console.log(Math.cos(180*Math.PI/180));    //-1
    </script>

    Math.min() method

    The min(a,b,c, d........n) method accepts a list of number arguments and return the minimum number value.

    Example

    <script>
        console.log(Math.min(12,34,2,354,35,64,63,632,67));   //2
    </script>

    Math.max() method

    The max(a,b,c, d........n) method accepts a list of number arguments and returns the maximum number value.

    Example

    <script>
        console.log(Math.max(12,34,2,354,35,64,63,632,67));   //632
    </script>

    Math.random() method

    The random() method returns a random number between o and 1, where 0 is inclusive and 1 is exclusive.

    Example

    <script>
        console.log(Math.random());   //0.31252884998756825
        console.log(Math.random());   //0.6562255988576251
        console.log(Math.random());   //0.8190211236154034
    </script>

    If you want to generate a random number between 0 and a specific number then you can multiply the random() method with the specified number, but the specified number will be exclusive.

    Example

    <script>
        // generate random number between 0 to 10(exclusive)
        console.log(Math.random()*10);   //9.143861799011312
        console.log(Math.random()*10);   //3.0111929359964784
        console.log(Math.random()*10);   //9.495673571174885
    </script>

    Math.log() method

    The log(num) method will return the natural logarithmic value of number num . The base for log() method is Math.E .

    Example

    <script>
        console.log(Math.log(1));   //0
    </script>

    Math.log2() method

    The log2(num) method will return the base 2 logarithmic value of number num . The base for log2() method is 2.

    Example

    <script>
        console.log(Math.log2(64));   //6
    </script>

    Math.log10() method

    The log10(num) method will return the base 10 logarithmic value of number num . The base for log10() method is 10.

    Example

    <script>
        console.log(Math.log10(100));   //2
    </script>

    Summary

    • Math is a JavaScript inbuilt object.
    • round() method round the number to its nearest integer value.
    • The ceil() method round up the number to its nearest integer value.
    • The floor() method round down the number to its nearest integer value.
    • The trunc() method removes the decimal point and only returns the integer value.
    • The pow(x,y) method returns the power of x to the y.
    • The sqrt() method returns the square root of the number.
    • The abs() method returns the positive value of the number.
    • The sin() and cos() method returns the sin and cosine value of the specified radian.